<?php 

include("../common.inc.php");

include("function_common.php");

include_once(INC_PATH."/delivery.class.php");

include_once(INC_PATH."/goodsforecast.class.php");

$d = new DeliveryClass();

$g = new GoodsForecastClass();



InitGP(array('page','action')); //初始化变量全局返回

if($action == 'edit'){

        InitGP(array('did', 'status'));

        $delivery = $d->getone($did);

        if(!$delivery){

                showmsg('查询不到对应运单', -1);

        }

        $gids = explode(',' ,$delivery['gids']);

        $goods = array();

        $allWeight = 0;

        foreach($gids as $gid){

                $good = $g->getone($gid);

                $goods[$gid] = $good;

                $allWeight = $good['weight'] + $allWeight;

        }

        if($status == 2){//执行订单重量审核操作

                InitGP(array('weight'));

                $data = array();

                $data['status'] = $status;

                $data['weight'] = $weight;
				
                if($d->edit($did, $data)){

                        $delivery = $d->getone($did);//从新获取详细

                }else{

                        showmsg('订单重量审核操作失败', -1);

                }

        }else if($status == 4){
                InitGP(array('price','weight', 'box_price', 'firm_price'));
                $data = array();
                $data['status'] = $status;
				$data['weight'] = $weight;
				$data['box_price'] = $box_price;
				$data['firm_price'] = $firm_price;
				$data['price'] = $price;
                if($d->edit($did, $data)){
                    $delivery = $d->getone($did);//从新获取详细
					//获取信息 email
					include_once(INC_PATH."/member.class.php");
					$m = new memberclass();
					$user = $m->getone($delivery["uid"] );
					if($user['email']){
						include_once(INC_PATH."/phpmailerice/sendmail.class.php");
						$mail = new SendMail();
						$users  = array();
						$users[$user['uname']] = $user['email'];
						$mail->setParameAndSend('订单已经核算 请尽快支付', $did. ' - 订单已经核算, 总价：'.$data['price'], $users);
					}
                }else{

                        showmsg('订单价格操作失败', -1);

                }

        }else if($status == 7){

                //执行发货

                InitGP(array('deliveryname','deliverynum'));

                $data = array();

                $data['status'] = $status;

                $data['deliveryname'] = $deliveryname;

                $data['deliverynum'] = $deliverynum;
				if($d->checkEx($deliveryname, $deliverynum)){
                    showmsg('快递单号不能重复', -1);
                }
                if($d->edit($did, $data)){
                    $delivery = $d->getone($did);//从新获取详细
					//获取信息 email
					include_once(INC_PATH."/member.class.php");
					$m = new memberclass();
					$user = $m->getone($delivery["uid"] );
					if($user['email']){
						include_once(INC_PATH."/phpmailerice/sendmail.class.php");
						$mail = new SendMail();
						$users  = array();
						$users[$user['uname']] = $user['email'];
						$mail->setParameAndSend('订单已经发货',$did. ' 已经发货，请注意查收！ 快递：'.$data['deliveryname'].' '.$data['deliverynum'],$users);
					}

                }else{

                        showmsg('发货失败', -1);

                }

        }else if($status == 10){

                InitGP(array('reply'));

                $data = array();

                $data['status'] = $status;

                $data['reply'] = $reply;

                $data['replytime'] = time();

                if($d->edit($did, $data)){

                        $delivery = $d->getone($did);//从新获取详细

                }else{

                        showmsg('回复成功', -1);

                }

        }

        $delivery = $d->getone($did);
		$allPrice = $delivery['price'] +  $delivery['box_price'] +  $delivery['firm_price'];
        $delivery['status_info'] = $d->getStatusInfo($delivery['status']);

        include("tpl/goods_delivery_edit.htm");

}else if($action == 'del'){
	 InitGP(array('did'));
	 $delivery = $d->getone($did);
	 if($delivery){
		if($delivery['status'] < 5){
			//删除订单
			$rs = $d->giddel($did);
			if($rs == 'OK'){
				$gids = explode(',' ,$delivery['gids']);
				foreach($gids as $gid){
					$dataarray['status'] = 3;
					//修改状态
					$g->edit($gid, $dataarray);
				}
				showmsg('订单删除成功！', 'delivery.php?status='.$delivery['status']);
			}else{
				showmsg('订单删除失败', -1);
			}
		}else{
			showmsg('订单当前状态不能取消', -1);
		}
	 }else{
		showmsg('订单信息不存在', -1);
	 }
}else if($action == 'send'){
		include_once(INC_PATH."/sendmail.class.php");
		$sendmail=new SendEmail();
		var_dump($sendmail->sendmailto('test','test','iceinto@163.com'));
}else{

        InitGP(array('page', 'status', 'uname')); //初始化变量全局返回

        if(intval($status)){

                $where = ' status = '.$status;

        }
		if($uname){
        	if($where){
        		$where .= ' AND ';
        	}
        	$where .= ' uname like \'%'.$uname.'%\'';
        }
        $Table = new TableClass("goods_delivery","did");

        AjaxHead();//禁止页面缓存

        $total = $Table->getcount();

        $pagesize = 15;                                                                                           //一页显示信息数

        $page = isset($page) ? max(1, intval($page)) : 1;             //处理页码变量

        $offset = ($page-1) * $pagesize;        //偏移量

        $orderstr = 'did DESC';

        $dataarray = $Table->getdata("$offset,$pagesize", $where, $orderstr);

        foreach ($dataarray as $key=>$ex){

                $dataarray[$key]['status_info'] = $d->getStatusInfo($ex['status']);

        }

        include("tpl/goods_delivery_list.htm");

}

?>